package com.linjiahao.oss.vo;

import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @ClassName BasicType
 * @Description: TODO
 * @Author linjiahao
 * @Date 15/12/2021
 **/
public enum BasicType {
    BYTE,
    SHORT,
    INT,
    INTEGER,
    LONG,
    DOUBLE,
    FLOAT,
    BOOLEAN,
    CHAR,
    CHARACTER,
    STRING;

    public static final Map<Class<?>, Class<?>> wrapperPrimitiveMap = new ConcurrentHashMap(8);
    public static final Map<Class<?>, Class<?>> primitiveWrapperMap = new ConcurrentHashMap(8);

    private BasicType() {
    }

    public static Class<?> wrap(Class<?> clazz) {
        if (null != clazz && clazz.isPrimitive()) {
            Class<?> result = (Class)primitiveWrapperMap.get(clazz);
            return null == result ? clazz : result;
        } else {
            return clazz;
        }
    }

    public static Class<?> unWrap(Class<?> clazz) {
        if (null != clazz && !clazz.isPrimitive()) {
            Class<?> result = (Class)wrapperPrimitiveMap.get(clazz);
            return null == result ? clazz : result;
        } else {
            return clazz;
        }
    }

    static {
        wrapperPrimitiveMap.put(Boolean.class, Boolean.TYPE);
        wrapperPrimitiveMap.put(Byte.class, Byte.TYPE);
        wrapperPrimitiveMap.put(Character.class, Character.TYPE);
        wrapperPrimitiveMap.put(Double.class, Double.TYPE);
        wrapperPrimitiveMap.put(Float.class, Float.TYPE);
        wrapperPrimitiveMap.put(Integer.class, Integer.TYPE);
        wrapperPrimitiveMap.put(Long.class, Long.TYPE);
        wrapperPrimitiveMap.put(Short.class, Short.TYPE);
        Iterator i$ = wrapperPrimitiveMap.entrySet().iterator();

        while(i$.hasNext()) {
            Map.Entry<Class<?>, Class<?>> entry = (Map.Entry)i$.next();
            primitiveWrapperMap.put(entry.getValue(), entry.getKey());
        }

    }
}
